import axios from 'axios'
import store from '../store'
import {
    Message,
    MessageBox
} from 'element-ui'
//const baseURL = 'http://localhost:9019/';//本地测试
// const baseURL = 'https://admin.bxjkw.cn/'

var  baseURL =  'https://admin.sxjgrd.cn/bxapi/hsprl/select/'
// var  baseURL =  'https://admin.bxjkw.cn/bxapi/hsprl/select/'
if(top.pathConfig && top.pathConfig.gateway){
    baseURL = top.pathConfig.gateway + 'bxapi/hsprl/select/'
}
// const baseURL = 'http://139.196.111.15:1810/hsprl/select/'
// const baseURL = 'http://192.168.0.154:8104/hsprl/select/'

const timeout = 120 * 1000;// 请求超时时间
// 创建axios实例
const service = axios.create({
    baseURL: baseURL,
    timeout: timeout // 请求超时时间
})
service.baseURL = baseURL;
service.timeout = timeout;
/*-------------------------------------------------request拦截器---------------------------------------------------------*/
service.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=UTF-8';
    config.headers['bx_auth_ticket'] = sessionStorage.getItem('bx_auth_ticket') || 'xabxdzkj-f544c510-2e03-46d8-9afa-a20ab2ed82d7'
    if (store.getters.token) {
        config.headers['token'] = encodeURIComponent(store.getters.token)
    }
    if (config.method === 'post') {
        config.data = JSON.stringify(config.data);
    }
    return config;
}, error => {// Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
})

/*-------------------------------------------------respone拦截器---------------------------------------------------------*/
service.interceptors.response.use(
    response => {
        /**
         * code为非0是抛错 可结合自己业务进行修改
         * 999:token过期;998:账号被禁用
         */
        console.log('response', response)
        const res = response.data
        const resCode = res.code;
        console.log('resCode', resCode)
        let boxMessage = res.msg;
        if (resCode > 0) {
            if (resCode === 200) {
                return response//放行
            }
            if (resCode === 500) {
                Message({
                    message: boxMessage,
                    type: 'error',
                    duration: 5 * 1000
                })
                return Promise.reject(res)
            }

            if (resCode === 999) {
                boxMessage = "当前登录信息已失效，请重新登录";
                Message({
                    message: boxMessage,
                    type: 'error',
                    duration: 5 * 1000
                })
                setTimeout(function () {
                    store.dispatch('FedLogOut').then(() => {//强制退出
                        location.reload()// 为了重新实例化vue-router对象 避免bug
                    })
                }, 5000)
            }

            if (resCode === 997) {
                Message({
                    message: 'token 非法',
                    type: 'error',
                    duration: 5 * 1000
                })
                return response//放行
            }

        } else {
            return response.data
        }
    },
    error => {
        console.log('err' + error) // for debug
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export function getdata(select,condition=[],pageNo=1,rownumber=1000,){
  return {
        "serviceName": select,
        "colNames": [
        "*"
        ],
        "condition": condition,
        "relation_condition": {},
        "page": {
          "pageNo": pageNo,
          "rownumber": rownumber
        }
  }
}





export default service   //暴露service实例，才可以被其他地方import引入
